<?php

function mysqldatetime_to_timestamp($datetime = "")
{
	// function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
	$l = strlen($datetime);
	if(!($l == 10 || $l == 19))
		return 0;

	//
	$date = $datetime;
	$hours = 0;
	$minutes = 0;
	$seconds = 0;

	// DATETIME only
	if($l == 19)
	{
		list($date, $time) = explode(" ", $datetime);
		list($hours, $minutes, $seconds) = explode(":", $time);
	}

	list($year, $month, $day) = explode("-", $date);

	return mktime($hours, $minutes, $seconds, $month, $day, $year);
}

/**
 * Convert MySQL's DATE (YYYY-MM-DD) or DATETIME (YYYY-MM-DD hh:mm:ss) to date using given format string
 *
 * Returns the date (format according to given string) of a given DATE/DATETIME
 *
 * @access    public
 * @return    integer
 */
function mysqldatetime_to_date($datetime = "", $format = "d/m/Y")
{
	return date($format, mysqldatetime_to_timestamp($datetime));
}

/**
 * Convert timestamp to MySQL's DATE or DATETIME (YYYY-MM-DD hh:mm:ss)
 *
 * Returns the DATE or DATETIME equivalent of a given timestamp
 *
 * @author Clemens Kofler <clemens.kofler@chello.at>
 * @access    public
 * @return    string
 */
function timestamp_to_mysqldatetime($timestamp = "", $datetime = true)
{
	if(empty($timestamp) || !is_numeric($timestamp)) $timestamp = time();

	return ($datetime) ? date("Y-m-d H:i:s", $timestamp) : date("Y-m-d", $timestamp);
}
